from rest_framework import serializers

from orders.models import Orders, PaymentMethodDDetails, OrdersMoney, Address, OrderItem, Invoice, \
    OrdersItemMoney, TaxCollection, ProductInfos, UpdateTime, CustomsDeclarationInfos, Infos, GoodsRank
from store.models import StoreManagement


class UpdateTimeSerializer(serializers.ModelSerializer):
    class Meta:
        model = StoreManagement
        fields = ['order_update_time']


class OrderItemMoneySerializer(serializers.ModelSerializer):
    class Meta:
        model = OrdersItemMoney
        fields = ['CurrencyCode', 'Amount']


class StoreSerializer(serializers.ModelSerializer):
    class Meta:
        model = StoreManagement
        fields = ['store', ]


class PaymentMethodDetailsSerializers(serializers.ModelSerializer):
    class Meta:
        model = PaymentMethodDDetails
        fields = ['PaymentMethodDetail']


class OrderTotalSerializers(serializers.ModelSerializer):
    class Meta:
        model = OrdersMoney
        fields = ['CurrencyCode', 'Amount']


class ShippingAddressSerializers(serializers.ModelSerializer):
    class Meta:
        model = Address
        # fields = '__all__'
        fields = ['City', 'PostalCode', 'isAddressSharingConfidential',
                  'StateOrRegion', 'Phone', 'CountryCode', 'Name',
                  'AddressLine1', 'AddressLine2', 'AddressLine3', 'AddressType',
                  'Country', 'District']


class UpdateMysqlOrdersSerializers(serializers.ModelSerializer):
    # PaymentMethodDetail = PaymentMethodDetailsSerializers
    # OrderTotal = OrderTotalSerializers
    # ShippingAddress = ShippingAddressSerializers
    # SellerStore = StoreSerializer

    class Meta:
        model = Orders
        fields = ['LatestShipDate', 'OrderType', 'PurchaseDate', "TQName",
                  'AmazonOrderId', 'BuyerEmail', 'IsReplacementOrder', 'LastUpdateDate', 'SellerStore',
                  'NumberOfItemsShipped', 'ShipServiceLevel', 'OrderStatus', 'SalesChannel', 'ShippedByAmazonTFM',
                  'IsBusinessOrder', 'LatestDeliveryDate', 'NumberOfItemsUnshipped', 'BuyerName',
                  'EarliestDeliveryDate', 'OrderTotal', 'IsPremiumOrder', 'EarliestShipDate', 'MarketplaceId',
                  'FulfillmentChannel', 'PaymentMethod', 'ShippingAddress', 'IsPrime', 'ShipmentServiceLevelCategory',
                  "MerchantOrderID", "MerchantFulfillmentID", ]


class UpdateMysqlOrdersSerializers2(serializers.ModelSerializer):
    # PaymentMethodDetail = PaymentMethodDetailsSerializers
    # OrderTotal = OrderTotalSerializers
    # ShippingAddress = ShippingAddressSerializers

    class Meta:
        model = Orders
        fields = ['LatestShipDate', 'OrderType', 'PurchaseDate', 'OrderTotal', 'BuyerName',
                  'AmazonOrderId', 'IsReplacementOrder', 'LastUpdateDate', "TQName",
                  'NumberOfItemsShipped', 'ShipServiceLevel', 'OrderStatus', 'SalesChannel',
                  'IsBusinessOrder', 'NumberOfItemsUnshipped',
                  'IsPremiumOrder', 'EarliestShipDate', 'MarketplaceId',
                  'FulfillmentChannel', 'IsPrime', 'ShipmentServiceLevelCategory', 'SellerStore',
                  "MerchantOrderID", "MerchantFulfillmentID", ]


class OrderItemDictSerializer(serializers.ModelSerializer):
    ItemPrice = OrderItemMoneySerializer(read_only=True)

    class Meta:
        model = OrderItem
        fields = ['Title', 'ASIN', 'SellerSKU', 'QuantityShipped', 'ItemPrice', 'OrderItemId']


class IsCustomsDecSerializer(serializers.ModelSerializer):
    class Meta:
        model = CustomsDeclarationInfos
        fields = ['id']


class OrderItemDictOfOneSerializer(serializers.ModelSerializer):
    ItemPrice = OrderItemMoneySerializer(read_only=True)
    ShippingPrice = OrderItemMoneySerializer(read_only=True)
    CustomsDeclarationInfo = IsCustomsDecSerializer(read_only=True)

    class Meta:
        model = OrderItem
        fields = ['Title', 'ASIN', 'SellerSKU', 'ConditionId', 'OrderItemId',
                  'QuantityShipped', 'ItemPrice', 'ShippingPrice', "GoodsImage",
                  'CustomsDeclarationInfo']


# class OrderListSerializer(serializers.ModelSerializer):
#     orderitem_set = OrderItemSerializer()
#     PaymentMethodDetails = PaymentMethodDetailsSerializers(read_only=True)
#     OrderTotal = OrderTotalSerializers(read_only=True)
#     ShippingAddress = ShippingAddressSerializers(read_only=True)
#     SellerStore = StoreSerializer(read_only=True)
#     class Meta:
#         model = Orders
#         fields = ['LatestShipDate', 'OrderType', 'PurchaseDate', 'Info',
#                   'AmazonOrderId', 'BuyerEmail', 'IsReplacementOrder', 'LastUpdateDate', 'SellerStore',
#                   'NumberOfItemsShipped', 'ShipServiceLevel', 'OrderStatus', 'SalesChannel', 'ShippedByAmazonTFM',
#                   'IsBusinessOrder', 'LatestDeliveryDate', 'NumberOfItemsUnshipped', 'PaymentMethodDetails', 'BuyerName',
#                   'EarliestDeliveryDate', 'OrderTotal', 'IsPremiumOrder', 'EarliestShipDate', 'MarketplaceId',
#                   'FulfillmentChannel', 'PaymentMethod', 'ShippingAddress', 'IsPrime', 'ShipmentServiceLevelCategory', 'TrackingNumber','orderitem_set']

class OrderItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderItem
        fields = ["GoodsImage", "Title", "ASIN", "SellerSKU", "ConditionId", "OrderItemId", "QuantityShipped",
                  "ShippingPrice", "CustomsDeclarationInfo"]
        depth = 1


class InfosSerializer(serializers.ModelSerializer):
    class Meta:
        model = Infos
        exclude = ('create_time', 'Order')
        depth = 1


class OrderListSerializer(serializers.ModelSerializer):
    orderitem_set = OrderItemSerializer(many=True)
    infos_set = InfosSerializer(many=True)

    class Meta:
        model = Orders
        fields = '__all__'
        depth = 1


class OrderItemOfOneSerializer(serializers.ModelSerializer):
    PaymentMethodDetails = PaymentMethodDetailsSerializers(read_only=True)
    OrderTotal = OrderTotalSerializers(read_only=True)
    ShippingAddress = ShippingAddressSerializers(read_only=True)
    SellerStore = StoreSerializer(read_only=True)
    OrderItemId = OrderItemDictOfOneSerializer(read_only=True)

    class Meta:
        model = Orders
        fields = ['OrderItemId', 'LatestShipDate', 'OrderType', 'PurchaseDate',
                  'AmazonOrderId', 'BuyerEmail', 'IsReplacementOrder', 'LastUpdateDate', 'SellerStore',
                  'NumberOfItemsShipped', 'ShipServiceLevel', 'OrderStatus', 'SalesChannel', 'ShippedByAmazonTFM',
                  'IsBusinessOrder', 'LatestDeliveryDate', 'NumberOfItemsUnshipped', 'PaymentMethodDetails',
                  'BuyerName',
                  'EarliestDeliveryDate', 'OrderTotal', 'IsPremiumOrder', 'EarliestShipDate', 'MarketplaceId',
                  'FulfillmentChannel', 'PaymentMethod', 'ShippingAddress', 'IsPrime', 'ShipmentServiceLevelCategory',
                  'TrackingNum']


class OrderStatuAndTrackingSeiralizer1(serializers.ModelSerializer):
    class Meta:
        model = Orders
        fields = ["OrderStatus", "Deal", "Examine", "Shipped"]

class OrderStatuAndTrackingSeiralizer(serializers.ModelSerializer):
    class Meta:
        model = Orders
        fields = ['OrderStatus']


class InvoiceDataSerializer(serializers.ModelSerializer):
    class Meta:
        model = Invoice
        fields = "__all__"


class TaxCollectionSerializer(serializers.ModelSerializer):
    class Meta:
        model = TaxCollection
        fields = ['Model', 'ResponsibleParty']


class ProductInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProductInfos
        fields = ['NumberOfItems', ]


class OrderItemsSerializer1(serializers.ModelSerializer):
    class Meta:
        model = OrderItem
        fields = ["TaxCollection", "QuantityOrdered", "Title", "ShippingTax", "PromotionDiscount",
                  "ConditionId", "IsGift", "ASIN", "SellerSKU", "OrderItemId", "ShippingDiscount",
                  "IsTransparency", "ProductInfo", "GiftWrapTax", "QuantityShipped", "ShippingPrice",
                  "GiftWrapPrice", "ConditionSubtypeId", "ItemPrice", "ItemTax", "ShippingDiscountTax",
                  "PromotionDiscountTax", "Order", 'MerchantOrderItemID', 'MerchantFulfillmentItemID', 'CODFee', 'CODFeeDiscount']


class OrderItemsSerializer3(serializers.ModelSerializer):
    class Meta:
        model = OrderItem
        fields = ["QuantityOrdered", "Title", "ShippingTax", "PromotionDiscount",
                  "ConditionId", "IsGift", "ASIN", "SellerSKU", "OrderItemId", "ShippingDiscount",
                  "IsTransparency", "ProductInfo", "GiftWrapTax", "QuantityShipped", "ShippingPrice",
                  "GiftWrapPrice", "ConditionSubtypeId", "ItemPrice", "ItemTax", "ShippingDiscountTax",
                  "PromotionDiscountTax", "Order", 'MerchantOrderItemID', 'MerchantFulfillmentItemID', 'CODFee', 'CODFeeDiscount']


class OrderItemsSerializer2(serializers.ModelSerializer):
    class Meta:
        model = OrderItem
        fields = ['QuantityOrdered', 'Title', "ConditionId", "IsGift", "ASIN", "Order",
                  "SellerSKU", "OrderItemId", "IsTransparency", "ProductInfo",
                  "QuantityShipped", "ConditionSubtypeId", 'MerchantOrderItemID', 'MerchantFulfillmentItemID'
            , 'ItemPrice', 'ShippingPrice',
                  'GiftWrapPrice', 'ItemTax', 'ShippingTax', 'GiftWrapTax',
                  'ShippingDiscountTax',
                  'ShippingDiscount', 'PromotionDiscountTax', 'PromotionDiscount', 'CODFee', 'CODFeeDiscount'
                  ]


class StoreForStoreNameSerializer(serializers.ModelSerializer):
    class Meta:
        model = StoreManagement
        fields = ["store"]


class OrderForStoreSerializer(serializers.ModelSerializer):
    SellerStore = StoreForStoreNameSerializer(read_only=True)

    class Meta:
        model = Orders
        fields = ["SellerStore"]


class OrderItemListSerializer(serializers.ModelSerializer):
    TaxCollection = TaxCollectionSerializer(read_only=True)
    ProductInfo = ProductInfoSerializer(read_only=True)
    ShippingTax = OrderItemMoneySerializer(read_only=True)
    PromotionDiscount = OrderItemMoneySerializer(read_only=True)
    ShippingDiscount = OrderItemMoneySerializer(read_only=True)
    GiftWrapTax = OrderItemMoneySerializer(read_only=True)
    ShippingPrice = OrderItemMoneySerializer(read_only=True)
    GiftWrapPrice = OrderItemMoneySerializer(read_only=True)
    ItemPrice = OrderItemMoneySerializer(read_only=True)
    ItemTax = OrderItemMoneySerializer(read_only=True)
    ShippingDiscountTax = OrderItemMoneySerializer(read_only=True)
    PromotionDiscountTax = OrderItemMoneySerializer(read_only=True)
    Order = OrderForStoreSerializer(read_only=True)

    class Meta:
        model = OrderItem
        fields = ["TaxCollection", "QuantityOrdered", "Title", "ShippingTax", "PromotionDiscount",
                  "ConditionId", "IsGift", "ASIN", "SellerSKU", "OrderItemId", "ShippingDiscount",
                  "IsTransparency", "ProductInfo", "GiftWrapTax", "QuantityShipped", "ShippingPrice",
                  "GiftWrapPrice", "ConditionSubtypeId", "ItemPrice", "ItemTax", "ShippingDiscountTax",
                  "PromotionDiscountTax", 'GoodsImage', 'Order']


class CustomsDeclarationInfosSerializer(serializers.ModelSerializer):
    class Meta:
        model = CustomsDeclarationInfos
        fields = ['ChineseCustomsDeclaration', 'EnglishCustomsDeclaration',
                  'Weight', 'Price', 'CustomsCode', 'IsElectric']


class CustomsDeclarationAddIdInfosSerializer(serializers.ModelSerializer):
    class Meta:
        model = CustomsDeclarationInfos
        fields = ['id', 'ChineseCustomsDeclaration', 'EnglishCustomsDeclaration',
                  'Weight', 'Price', 'CustomsCode', 'IsElectric']


class OrderItemForCustomsDeclarationInfosSerializer(serializers.ModelSerializer):
    CustomsDeclarationInfo = CustomsDeclarationInfosSerializer(read_only=True)

    class Meta:
        model = OrderItem
        fields = ['CustomsDeclarationInfo', 'Title', "GoodsImage"]


class OrderItemIdSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderItem
        fields = ['CustomsDeclarationInfo', 'Title', "GoodsImage"]


"""云图物流序列化器群"""


class AdressSerializer(serializers.ModelSerializer):
    class Meta:
        model = Address
        fields = "__all__"


class OrderAddressSerializer(serializers.ModelSerializer):
    ShippingAddress = AdressSerializer

    class Meta:
        model = Orders
        fields = ['ShippingAddress']


class CustomsDecSerializer(serializers.ModelSerializer):
    class Meta:
        model = CustomsDeclarationInfos
        fields = "__all__"


class OrderItemCustomDec(serializers.ModelSerializer):
    class Meta:
        model = OrderItem
        fields = ["CustomsDeclarationInfo", 'QuantityOrdered']


class TrackingNumSerializer(serializers.ModelSerializer):
    class Meta:
        model = Orders
        fields = ['TrackingNumber']


class OrderInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Infos
        fields = ['Order', 'Info', 'username']

    def create(self, validated_data):
        return Infos.objects.create(**validated_data)

    def update(self, instance, validated_data):
        instance.Order = validated_data.get('Order', instance.Order)
        instance.Info = validated_data.get('Info', instance.Info)
        instance.username = validated_data.get('username', instance.username)
        instance.save()
        return instance


class ExamineSerializer(serializers.ModelSerializer):
    class Meta:
        model = Orders
        fields = ['Examine']


class DealSerializer(serializers.ModelSerializer):
    class Meta:
        model = Orders
        fields = ['Deal']


class ApplicationOrderNumberSerializer(serializers.ModelSerializer):
    class Meta:
        model = Orders
        fields = ['ApplicationOrderNumber']


class FaceSheetSerializer(serializers.ModelSerializer):
    class Meta:
        model = Orders
        fields = ['FaceSheet']


class ShippedSerializer(serializers.ModelSerializer):
    class Meta:
        model = Orders
        fields = ['Shipped']


"""以下三个确认订单上传序列化器"""


class OrderFulfillmentItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderItem
        fields = ["MerchantOrderItemID", "MerchantFulfillmentItemID", "QuantityOrdered"]
        depth = 1


class OrderFulfillmentSerializer(serializers.ModelSerializer):
    orderitem_set = OrderFulfillmentItemSerializer(many=True)

    class Meta:
        model = Orders
        fields = ["MerchantOrderID", "MerchantFulfillmentID", "TrackingNumber", "orderitem_set"]
        depth = 1


class OrderFulfillmentStoreManagementSerializer(serializers.ModelSerializer):
    class Meta:
        model = StoreManagement
        fields = ["seller_id", "mws_token"]


"""以下三个导表序列化器"""


class ExportOrderItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderItem
        fields = ["ASIN", "SellerSKU", "OrderItemId", "Title", "QuantityShipped", "GoodsImage",
                  "CustomsDeclarationInfo"]
        # fields = '__all__'
        depth = 1


class ExportInfosSerializer(serializers.ModelSerializer):
    class Meta:
        model = Infos
        fields = ["id", "create_time", "update_time", "Info", "username"]


class ExportOrdersSerializer(serializers.ModelSerializer):
    orderitem_set = ExportOrderItemSerializer(many=True)
    infos_set = ExportInfosSerializer(many=True)

    class Meta:
        model = Orders
        fields = '__all__'
        depth = 1


"""更新商品排名序列化器"""


class GoodsRankSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsRank
        fields = "__all__"

    def create(self, validated_data):
        """新建"""
        return GoodsRank.objects.create(**validated_data)


"""商品排名序列化器"""
class GoodsFinanceViewSerializer(serializers.ModelSerializer):
    class Meta:
        model = Orders
        fields = "__all__"

"""商品详情序列化器"""

class GoodsItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderItem
        # fields = ["ASIN", "SellerSKU", "OrderItemId", "Title", "QuantityShipped", "GoodsImage",
        #           "CustomsDeclarationInfo"]
        fields = '__all__'
        depth = 2


class ShippingAddressSerializers1(serializers.ModelSerializer):
    """修改收货地址"""
    class Meta:
        model = Address
        # fields = '__all__'
        fields = ['PostalCode', 'Phone', 'CountryCode', 'Name', 'City',
                  'AddressLine1', 'AddressLine2', 'AddressLine3']


class TQNameSerializer(serializers.ModelSerializer):
    class Meta:
        model = Orders
        fields = ['TQName', "TrackingNumber",]

class TrackingNumberSerializer(serializers.ModelSerializer):
    class Meta:
        model = Orders
        fields = ["TrackingNumber"]

